﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace TimeSheet.UI.Views
{
    /// <summary>
    /// Control to display a week timesheet with all project times in Items property
    /// </summary>
    public partial class WeekControl : UserControl, INotifyPropertyChanged
    {
        #region EVENTS

        /// <summary>
        /// Raise a property changed
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region DECLARATIONS

        private DateTime _weekDate = DateTime.Today;
        
        #endregion

        #region CONSTRUCTORS

        /// <summary>
        /// Initializes a new instance of this control
        /// </summary>
        public WeekControl()
        {
            InitializeComponent();

            this.Projects = new ObservableCollection<WeekProjectView>();
            this.WeekDate = DateTime.Today;
            this.DayFooter = new WeekDayFooter(this.Projects);
            this.DataContext = this;
        }

        #endregion

        #region PROPERTIES

        /// <summary>
        /// Gets or sets all project times, for each days
        /// </summary>
        public ObservableCollection<WeekProjectView> Projects { get; set; }

        /// <summary>
        /// Sets or gets the footer displayed in the bottom screen
        /// </summary>
        public WeekDayFooter DayFooter { get; set; }
        
        /// <summary>
        /// Gets or sets a date included in this week
        /// </summary>
        public DateTime WeekDate
        { 
            get 
            { 
                return _weekDate; 
            }
            set
            {
                _weekDate = value;
                OnPropertyChanged("WeekDate");
                OnPropertyChanged("DayLabel");
            }
        }
        
        /// <summary>
        /// Gets all day label 
        /// </summary>
        public WeekDayLabelView DayLabel
        {
            get
            {
                return new WeekDayLabelView(this.WeekDate);
            }
        }

        #endregion

        #region PRIVATES

        /// <summary>
        /// Populate the project textbox dropdown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AutoCompleteBox_Populating(object sender, PopulatingEventArgs e)
        {
            AutoCompleteBox box = sender as AutoCompleteBox;

            if (box != null)
            {
                box.ItemsSource = new string[] { " " + DateTime.Now.ToLongTimeString(), "Un", "Deux", "Trois", "Quatre", "Cinq", "Un", "Deux", "Trois", "Quatre", "Cinq", "Un", "Deux", "Trois", "Quatre", "Cinq", "Un", "Deux", "Trois", "Quatre", "Cinq", "Un", "Deux", "Trois", "Quatre", "Cinq", "Un", "Deux", "Trois", "Quatre", "Cinq" };
                box.PopulateComplete();
            }
        } 

        /// <summary>
        /// Display all project in the dropdown and display it
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AutoCompleteBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Down)
            {
                AutoCompleteBox box = sender as AutoCompleteBox;
                box.IsDropDownOpen = true;
            }
        }

        /// <summary>
        /// Resize all controls (txtprojects and time box)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            double projectWidth = this.ActualWidth - 7 * 70 - 30;

            if (projectWidth > 0)
            {
                panHeader.Width = this.ActualWidth;
                lblProjects.Width = projectWidth;
                lblTotal.Width = projectWidth;

                foreach (WeekProjectView item in this.Projects)
                {
                    item.Width = lblProjects.Width;
                }
            }
        }

        /// <summary>
        /// Raise the PropertyChanged event
        /// </summary>
        /// <param name="propertyName"></param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Select all text when textbox receive the focus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox text = sender as TextBox;
            text.SelectAll();
        }

        #endregion
        
    }

}
